package com.software.sqlite.service.impl;
import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.software.sqlite.service.IYstMovieService;
import com.software.sqlite.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class YstMovieService implements IYstMovieService {

    @Value("${film.cloud.url}")
    private String filmCloudUrl;


    @Override
    public String apiTest(String type,String requestData) {
        String result="";
        try {

        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String authLoginByCloud(String AuthKey, String AppKey) {
        String result="";
        try {
            HttpRequest request = HttpUtil.createPost(filmCloudUrl+"/V1/Auth/login");
            Map<String, String> param = new HashMap();
            param.put("AuthKey", AuthKey);
            param.put("AppKey", AppKey);
            String body = JSON.toJSONString(param);
            request.body(body);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getFilmJsonFileByCloud(String token) {
        String result="";
        try {
            HttpRequest request = HttpUtil.createGet(filmCloudUrl+"/V1/Film/getJsonfile");
            request.header("token", token);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getFreighterListByCloud(String token) {
        String result="";
        try {
            HttpRequest request = HttpUtil.createGet(filmCloudUrl+"/V1/Freighter/getFreighterList");
            request.header("token", token);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getFreighterFilmByCloud(String token,String ShopID,String ClientID) {
        String result="";
        try {
            HttpRequest request = HttpUtil.createGet(filmCloudUrl+"/V1/Freighter/getFreighterFilm?ShopID="+ShopID+"&ClientID="+ClientID);
            request.header("token", token);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getRegitsterDeviceShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * 	"C": "Device",
             * "D": {
             * "IP": "192.168.1.1",
             * "MAC": "DD:DD:DD:DD:DD:DD",
             * "ModelNo": "BOX-002",
             * "Title": "BOX-002",
             * "UniqueCode": "b562e3d8a4314e9e9ae6c3648855b2e2",
             * "Version": 1.23
             * },
             * "F": "Regdevice",
             * "M": "DD:DD:DD:DD:DD:DD",
             * "U": "b562e3d8a4314e9e9ae6c3648855b2e2"
             * }
             */
            HttpRequest request = HttpUtil.createPost(shipServerUrl+"/device.api");
            request.setConnectionTimeout(5000);
            String body = JSON.toJSONString(params);
            request.body(body);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getSettingByShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * 	"C": "Device",
             * "D": {
             * "IP": "192.168.1.1",
             * "MAC": "DD:DD:DD:DD:DD:DD",
             * "ModelNo": "BOX-002",
             * "Title": "BOX-002",
             * "UniqueCode": "b562e3d8a4314e9e9ae6c3648855b2e2",
             * "Version": 1.23
             * },
             * "F": "Regdevice",
             * "M": "DD:DD:DD:DD:DD:DD",
             * "U": "b562e3d8a4314e9e9ae6c3648855b2e2"
             * }
             */
            HttpRequest request = HttpUtil.createPost(shipServerUrl+"/device.api");
            request.setConnectionTimeout(5000);
            String body = JSON.toJSONString(params);
            request.body(body);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getGroupTypeListByShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * 	"mac": "DD:DD:DD:DD:DD:DD",
             *  "filmsource": "2"
             * }
             */
            HttpRequest request = HttpUtil.createPost(shipServerUrl+"/sauna/index.php/SmartTvAPI/API/index/Payfilm/payfilm/getGroupTypeList");
            request.form(params);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getFilmListByTypeByShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * 	"mac": "DD:DD:DD:DD:DD:DD",
             *  "filmsource": "2",
             *  "leixing": "2",
             *  "diqu":"0",
             *  "shijian":"0"
             * }
             */
            HttpRequest request = HttpUtil.createPost(shipServerUrl+"/sauna/index.php/SmartTvAPI/API/index/Payfilm/payfilm/getFilmListByType");
            request.form(params);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getFilmDetailByShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * 	"mac": "DD:DD:DD:DD:DD:DD",
             *  "filmsource": "2",
             *  "movieId": 1111
             * }
             */
//            HttpRequest request = HttpUtil.createGet(shipServerUrl+"/zhdx.api/film/getFilmDetail");
//            String body = JSON.toJSONString(params);
//            request.body(body);
//            HttpResponse execute = request.execute();
//            if (!execute.isOk()) {
//                log.error("请求token失败,body={},execute={}", execute.body(), execute);
//            }
            String mac= CommonUtils.parseStringData(params.get("mac"));
            String filmsource= CommonUtils.parseStringData(params.get("filmsource"));
            String movieId= CommonUtils.parseStringData(params.get("movieId"));
            HttpRequest request = HttpUtil.createGet(shipServerUrl+"/zhdx.api/film/getFilmDetail?mac="+mac+"&filmsource="+filmsource+"&movieId="+movieId);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getPlayUrlByShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * 	"mac": "DD:DD:DD:DD:DD:DD",
             *  "filmsource": "2",
             *  "movieId": 1111
             * }
             */
            HttpRequest request = HttpUtil.createGet(shipServerUrl+"/zhdx.api/film/getPlayUrl");
            request.setConnectionTimeout(5000);
            request.form(params);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String searchFilmByShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * 	"mac": "DD:DD:DD:DD:DD:DD",
             *  "filmsource": "2",
             *  "show_model": "1",
             *  "keyword": "dy"
             * }
             */
            HttpRequest request = HttpUtil.createPost(shipServerUrl+"/sauna/index.php/SmartTvAPI/API/index/Payfilm/payfilm/searchFilm");
            request.form(params);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getRecommendFilmByShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * 	"mac": "DD:DD:DD:DD:DD:DD",
             *  "filmsource": "2",
             *  "size": "10"
             * }
             */
            HttpRequest request = HttpUtil.createPost(shipServerUrl+"/sauna/index.php/SmartTvAPI/API/index/Payfilm/payfilm/getRecommendFilm");
            request.form(params);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String upPlayStateByShipServer(String shipServerUrl, Map<String, Object> params) {
        String result="";
        try {
            /**
             * {
             * "filmId": "16604",
             * "title": "⻘⽊劫",
             * "countTag": "16604abcde",
             * "duration": "500",
             * "lastPos": "12",
             * "mac": "28:35:45:95:50:A7",
             * "watchLen": "123"
             * }
             */
            HttpRequest request = HttpUtil.createPost(shipServerUrl+"/zhdx.api/film/upPlayState");
            String body = JSON.toJSONString(params);
            request.body(body);
            HttpResponse execute = request.execute();
            if (!execute.isOk()) {
                log.error("请求token失败,body={},execute={}", execute.body(), execute);
            }
            result= UnicodeUtil.toString(execute.body());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
